package ControlLayer; //makes this class a part of the ControlLayer package
import ModelLayer.*;
import java.util.*;

/**
 * 
 * Controller class for the DVD collection
 * <p>
 * Accepts input user data from the <code>DvdUI</code>(TUI Layer) and manages the data stored in <code>DVD</code> and <code>Copy</code>(ModelLayer).
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 * <p>
 * <b><i>History:</i></b>
 * <p>
 * (KBH): 2005.03.31:
 * <p>
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering and loose coupling.
 * 
 */
public class DVDCtr
{
    private ArrayList<DVD> dvdList;
    
    /**
     * Constructor assigns a collection of DVDs to the dvdList.  
     * @param backup stores a recently created new empty collection of DVDs or a backup of a collection managed previously. 
     */
    public DVDCtr(ArrayList<DVD> backup)
    {
        dvdList = backup;  
    }
    
    /**
     * Finds and returns a dvd by its ID.
     * @param no keeps the ID of the DVD.
     * @return object of type DVD if the DVD exists and <code>null</code> if there is no such DVD with this ID.
     */
    public DVD findDVD(long no)
    {
        Iterator<DVD> it = dvdList.iterator();
        while(it.hasNext())
        {
            DVD test = it.next();
            if(test.getId() == no) return test;
        }
        return null;
    }
    
    /**
     * Finds and returns a copy by its ID.
     * @param dvd stores an object of type dvd.
     * @param no stores the serial number of desired copy.
     * @return object of type Copy if such DVD Copy exists and <code>null</code> if there is no such DVD Copy with this Srial Number or there are no copies of this DVD.
     */
    public Copy findCopy(DVD dvd,long no)
    {
        ArrayList<Copy> copylist = dvd.getCopyList();
        Iterator<Copy> it = copylist.iterator();
        while(it.hasNext())
        {
            Copy test = it.next();
            if(test.getSerialNumber() == no) return test;
        }
        return null;
    }
    
    /**
     * Creates new DVD and adds it to the collection.
     * @param id takes a number of type long and stores the id of the DVD.
     * @param title stores a character string of the title of the DVD.
     * @param artist stores a character string of the artist of the DVD.
     * @param publicationDate stores a character string of the Publication Date of the DVD.
     */
    public void createDVD(long id, String title, String artist, String publicationDate)
    {       
        DVD newDVD = new DVD(id,title,artist,publicationDate);
        dvdList.add(newDVD);
    }
    
    /**
     * Updates an existing DVD, which is found by id, by editing all its fields.
     * @param id takes a number of type long and stores the id of an existing DVD.
     * @param newid takes a number of type long as a new id for the DVD.
     * @param title stores a character string of the new title of the DVD.
     * @param artist stores a character string of the new artist of the DVD.
     * @param publicationDate stores a character string of the new Publication Date of the DVD.
     */
    public void updateDVD(long id, long newid, String title, String artist, String publicationDate)
    {
        Iterator<DVD> it = dvdList.iterator();
        while(it.hasNext())
        {
            DVD toupdate = it.next();
            if(toupdate.getId() == id)
            {
                toupdate.setId(newid);
                toupdate.setTitle(title);
                toupdate.setArtist(artist);
                toupdate.setDate(publicationDate);
            }
        }
    }
    
    /**
     * Updates an existing DVD Copy, which is found by Serial Number, by editing all its fields.
     * @param dvd          stores the specific dvd whose copies are going to be checked for the desired one.
     * @param id           takes a number of type long and stores the Serial Number of an existing DVD Copy.
     * @param newid        takes a number of type long as a new Serial Number for the DVD Copy.
     * @param purchaseDate stores a character string of the new Purchase Date of the DVD Copy.
     * @param purchaseCost stores a number of type int for the new Purchase Cost of the DVD Copy.
     */
    public void updateDVDCopy(DVD dvd, long id, long newid, String purchaseDate, int purchaseCost)
    {
        ArrayList<Copy> copylist = dvd.getCopyList();
        Iterator<Copy> it = copylist.iterator();
        while(it.hasNext())
        {
            Copy toupdate = it.next();
            if(toupdate.getSerialNumber() == id)
            {
                toupdate.setSerialNumber(newid);
                toupdate.setPurchaseDate(purchaseDate);
                toupdate.setPurchaseCost(purchaseCost);
            }
        }
    }
    
    /**
     * Deletes a DVD by using its id number.
     * @param id takes a number in the range of type long that is used as an desired id.
     */
    public void deleteDVD(long id)
    {
        Iterator<DVD> it = dvdList.iterator();
        while(it.hasNext())
        {
            DVD todelete = it.next();
            if(todelete.getId() == id) it.remove();
        }
    }
    
    /**
     * Creates a copy that is attributed to an existing DVD
     * @param id            stores a number that should be an exisiting id of a DVD.
     * @param serialNumber  stores a character string that will be the Serial Number of a new DVD Copy.
     * @param pruchaseDate  stores a character string that will be the Purchase Date of a new DVD Copy.
     * @param purchaseCost  stores a character string that will be the Purchase Cost of a new DVD Copy.
     */
    public void createCopy(long id, long serialNumber, String purchaseDate, int purchaseCost)
    {
        Copy newcopy = new Copy(serialNumber, purchaseDate, purchaseCost);
        Iterator<DVD> it = dvdList.iterator();
        while(it.hasNext())
        {
            DVD test = it.next();
            if(test.getId() == id) 
            { 
                test.addCopy(newcopy);
            }
        }
    }
    
    /**
     * Returns all DVDs in the current list.
     * @return an arraylist containing all dvd obejcts in the collection. 
     */
    public ArrayList<DVD> listAllDvds()
    {
         return dvdList;
    }
}
